/*
* This file is part of RoboDS (http://code.google.com/p/robods/)
* Copyright (C) 2007 Blakharaz
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/

#include "DsUserInterface.h"

#include <PA9.h>

#include "DsCard.h"
#include "DsSprite.h"
#include "Logger.h"
#include "Map.h"
#include "OkButton.h"
#include "Robot.h"

namespace robo
{

    DsUserInterface::DsUserInterface(Robot* robot)
    :	UserInterface(robot)
    {
	    for (int idx = 0; idx < 5; ++idx)
	    {
		    mSlots[idx].Top = 6;
		    mSlots[idx].Bottom = 53;
		    mSlots[idx].Left = 39 + 44*idx;
		    mSlots[idx].Right = 71 + 44*idx;
	    }
	    mSlots[SLOT_AVAIL].Top = 67;
	    mSlots[SLOT_AVAIL].Bottom = 122;
	    mSlots[SLOT_AVAIL].Left = 37;
	    mSlots[SLOT_AVAIL].Right = 249;

        mPowerButton = new DsSprite(DsSprite::SPRITE_POWER_BUTTON, 0);
        mPowerButton->setPosition(160, 128);
        PA_SetSpriteAnim(0, mPowerButton->createSprite(), 0);
     
        mOkButton = new OkButton();
        mOkButton->setPosition(192, 128);
	    mOkButton->setEnabled(false);
    }

    DsUserInterface::~DsUserInterface()
    {
    }

    void DsUserInterface::update()
    {
        mMap->update();
        mRobot->update();

        // Draw the selected cards
        for (int idx = 0; idx < 5; ++idx)
        {
            if (mSelectedCard[idx])
            {
			    dynamic_cast<DsCard*>(mSelectedCard[idx])->setPosition(
				    mSlots[idx].Left, 
				    mSlots[idx].Top);
            }
        }

        // Draw the drawn cards
	    static unsigned short CARD_WIDTH = 32;

	    unsigned short cardDist = 
		    mAvailableCards.size() <= 1 
		    ? 0
		    : (mSlots[SLOT_AVAIL].Right - CARD_WIDTH - mSlots[SLOT_AVAIL].Left) 
			    / (mAvailableCards.size() - 1);

        unsigned short drawnIdx = 0;

	    char buf[32];
	    sprintf(buf, "%d-%d: %d", mSlots[SLOT_AVAIL].Right, mSlots[SLOT_AVAIL].Left, cardDist);
	    Logger::getSingleton()->log(buf);

        for (CardSet::const_iterator it = mAvailableCards.begin();
            it != mAvailableCards.end(); ++it)
        {
		    DsSprite* cur = dynamic_cast<DsSprite*>(*it);
    		
            cur->setPosition(
			    mSlots[SLOT_AVAIL].Right - CARD_WIDTH - drawnIdx * cardDist, 
			    mSlots[SLOT_AVAIL].Top);

            drawnIdx++;
        }
    }

    int DsUserInterface::getCardSlotAt(u16 x, u16 y)
    {
	    for (int idx = 0; idx <= 5; ++idx)
        {
		    Rectangle r = mSlots[idx];
            if (r.Left <= x && x <= r.Right
			    && r.Top <= y && y <= r.Bottom)
            {
			    return idx;
            }
        }

	    return NO_SLOT;
    }

    void DsUserInterface::checkOkButton()
    {
	    bool okEnabled = true;
	    for (int idx = 0; idx < 5; ++idx)
        {
		    if (!mSelectedCard[idx])
		    {
			    okEnabled = false;
			    break;
		    }
	    }

	    mOkButton->setEnabled(okEnabled);
    }

    bool DsUserInterface::isPowerButton(DsSprite* sprite) const
    {
	    return sprite == mPowerButton;
    }

    void DsUserInterface::togglePowerButton()
    {
	    if (mPowerEnabled)
	    {
		    mPowerEnabled = false;
		    Logger::getSingleton()->log("Power disabled");
		    PA_SetSpriteAnim(0, mPowerButton->createSprite(), 1);
	    }
	    else
	    {
		    mPowerEnabled = true;
		    Logger::getSingleton()->log("Power enabled");
		    PA_SetSpriteAnim(0, mPowerButton->createSprite(), 0);
	    }
    }

    OkButton* DsUserInterface::getOkButton() const
    {
	    return mOkButton;
    }

} // namespace robo
